<?php
class LeaderBoardInfo{
	private $Title = array();
	private $ID = array();
	function __construct($id,$title){
		$this->ID = unpack('C*', $id ) ;
		$this->Title = unpack('C*', $title ) ;
	}
	static function swap($bytebuffer){
		$obj = new LeaderBoardInfo(implode(array_map("chr",  DataUtil::readBBin( $bytebuffer )  )) ,implode(array_map("chr",  DataUtil::readBBin( $bytebuffer )  )) );
		return $obj;
	}
	function getID(){
		return implode(array_map("chr",  $this->ID )) ;
	}
	function getTitle(){
		return implode(array_map("chr",  $this->Title )) ;
	}
	function setTitle($title){
		$this->Title = unpack('C*', $title ) ;
	}
	function setID($id){
		$this->ID = unpack('C*', $id ) ;
	}
	function getBinSize(){
		$size = 0;
		$size += 1  + count($this->ID);
		$size += 1  + count($this->Title);
		return $size;
	}
	function getBinary(){
		$buf = ByteBuffer::allocate( $this->getBinSize() ) ;
		DataUtil::writeBBin( $buf , $this->ID ) ;
		DataUtil::writeBBin( $buf , $this->Title ) ;
		return  $buf ->_array() ;
	}
}
class DataGameInfo{
	private $SecretKey = array();
	private $Name = array();
	private $LeaderBoards = array();
	private $Infomation = array();
	private $FbAppId = 0;
	function __construct(){
		$this->SecretKey = unpack('C*', UtilHash::generateKey( 32 )  ) ;
		$this->FbAppId = 0;
	}
	static function swap($bytebuffer){
		$obj = new DataGameInfo();
		$obj->SecretKey = DataUtil::readBBin( $bytebuffer ) ;
		$obj->Name = DataUtil::readBBin( $bytebuffer ) ;
		$nof_leaderboard = DataUtil::readInt8( $bytebuffer ) ;
		for($i = 0; $i < $nof_leaderboard; $i++){
			$obj->LeaderBoards[] = LeaderBoardInfo::swap($bytebuffer);
		}
		$obj->Infomation = DataUtil::readSBin( $bytebuffer ) ;
		$obj->FbAppId = DataUtil::readInt32( $bytebuffer ) ;
		return $obj;
	}
	function getSecretKey(){
		return implode(array_map("chr",  $this->SecretKey )) ;
	}
	function getName(){
		return implode(array_map("chr",  $this->Name )) ;
	}
	function setName($name){
		$this->Name = unpack('C*', $name ) ;
	}
	function getInfomation(){
		return implode(array_map("chr",  $this->Infomation )) ;
	}
	function setInfomation($infomation){
		$this->Infomation = unpack('C*', $infomation ) ;
	}
	function getNumOfLeaderBoard(){
		return count($this->leaderBoards);
	}
	function addLeaderBoard($board){
		$this->LeaderBoards[] = $board;
	}
	function getLeaderBoards(){
		return $this->LeaderBoards;
	}
	function setFbAppId($fbappid){
		$this->FbAppId = $fbappid;
	}
	function getFbAppId(){
		return $this->FbAppId;
	}
	function getBinSize(){
		$size = 0;
		$size += 1  + count($this->SecretKey);
		$size += 1  + count($this->Name);
		$size += 1 ;
		if(is_array($this->LeaderBoards)){
			foreach($this->LeaderBoards as $board){
				$size += $board->getBinSize();
			}
		}
		$size += 2  + count($this->Infomation);
		$size += 4 ;
		return $size;
	}
	function getBinary(){
		$buf = ByteBuffer::allocate( $this->getBinSize() ) ;
		DataUtil::writeBBin( $buf , $this->SecretKey ) ;
		DataUtil::writeBBin( $buf , $this->Name ) ;
		DataUtil::writeInt8( $buf , count($this->LeaderBoards) ) ;
		if(is_array($this->LeaderBoards)){
			foreach($this->LeaderBoards as $board){
				DataUtil::writeBytes( $buf , $board->getBinary() ) ;
			}
		}
		DataUtil::writeSBin( $buf , $this->Infomation ) ;
		DataUtil::writeInt32( $buf ,  $this->FbAppId ) ;
		return  $buf ->_array() ;
	}
}
?>